namespace iScadaFBD
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Reflection;

    [Serializable]
    public class ObjList
    {
        private ArrayList objList = new ArrayList();
        private ArrayList tempList = new ArrayList();

        public void AddObject(DrawObject o)
        {
            this.objList.Add(o);
        }

        public void Clone(DrawArea drawArea)
        {
            int selectionCount = this.SelectionCount;
            new ArrayList().Clear();
            for (int i = 0; i < selectionCount; i++)
            {
                DrawObject selectedObject = this.GetSelectedObject(i);
                Point point = new Point(selectedObject.FBRect.X + 10, selectedObject.FBRect.Y + 10);
                selectedObject.Selected = false;
                if (selectedObject.GetType().Name == "AND_FB")
                {
                    drawArea.ActivedFBType = GlobalParam.FBType.AND;
                    AND_FB and_fb = new AND_FB(point, drawArea);
                    and_fb.Selected = true;
                    this.objList.Add(and_fb);
                }
            }
            drawArea.ActivedFBType = GlobalParam.FBType.POINTER;
        }

        public int Count()
        {
            return this.objList.Count;
        }

        public void DeleteConnection()
        {
            for (int i = this.tempList.Count - 1; i >= 0; i--)
            {
                for (int j = this.objList.Count - 1; j >= 0; j--)
                {
                    if (((DrawObject) this.objList[j]).ObjectType == GlobalParam.ObjectType.Connection)
                    {
                        Connection connect = (Connection) this.objList[j];
                        Connection connection2 = (Connection) this.tempList[i];
                        if (connect == connection2)
                        {
                            this.ResetNode(connect);
                            this.objList.RemoveAt(j);
                        }
                    }
                }
            }
            this.tempList.Clear();
        }

        public bool DeleteFB()
        {
            bool flag = false;
            for (int i = this.objList.Count - 1; i >= 0; i--)
            {
                if (((DrawObject) this.objList[i]).Selected)
                {
                    FBObject obj2 = (FBObject) this.objList[i];
                    this.Exeorder(obj2.ExecuteOrder);
                    GlobalParam.order--;
                    this.objList.RemoveAt(i);
                    flag = true;
                }
            }
            return flag;
        }

        public bool DeleteSelection()
        {
            bool flag = false;
            for (int i = this.objList.Count - 1; i >= 0; i--)
            {
                if (((DrawObject) this.objList[i]).Selected)
                {
                    if (((DrawObject) this.objList[i]).ObjectType == GlobalParam.ObjectType.FunctionBlock)
                    {
                        this.GetConnection((FBObject) this.objList[i]);
                    }
                    else
                    {
                        if (((DrawObject) this.objList[i]).ObjectType == GlobalParam.ObjectType.Connection)
                        {
                            Connection connect = (Connection) this.objList[i];
                            this.ResetNode(connect);
                        }
                        this.objList.RemoveAt(i);
                    }
                    flag = true;
                }
            }
            this.DeleteConnection();
            return this.DeleteFB();
        }

        public void Draw(Graphics g, DrawArea drawArea)
        {
            this.MoveSelectionToFront();
            int count = this.objList.Count;
            for (int i = 0; i <= (count - 1); i++)
            {
                DrawObject obj2 = (DrawObject) this.objList[i];
                obj2.Draw(g, drawArea);
                if (obj2.Selected)
                {
                    obj2.DrawTracker(g, drawArea);
                }
            }
        }

        public void Exeorder(int c)
        {
            for (int i = this.objList.Count - 1; i >= 0; i--)
            {
                if (((DrawObject) this.objList[i]).ObjectType == GlobalParam.ObjectType.FunctionBlock)
                {
                    FBObject obj2 = (FBObject) this.objList[i];
                    if (obj2.ExecuteOrder > c)
                    {
                        obj2.ExecuteOrder--;
                    }
                }
            }
        }

        public bool FBIsInList(FBObject fb)
        {
            bool flag = false;
            int count = this.objList.Count;
            for (int i = 0; i < count; i++)
            {
                if (((DrawObject) this.objList[i]).ObjectType == GlobalParam.ObjectType.FunctionBlock)
                {
                    FBObject obj2 = (FBObject) this.objList[i];
                    if (obj2.ID == fb.ID)
                    {
                        flag = true;
                    }
                    return flag;
                }
            }
            return flag;
        }

        public void GetConnection(FBObject o)
        {
            int count = o.AttachConnections.Count;
            for (int i = 0; i < count; i++)
            {
                int num3 = this.objList.Count;
                Connection connection = (Connection) o.AttachConnections[i];
                for (int k = 0; k < num3; k++)
                {
                    if (((DrawObject) this.objList[k]).ObjectType == GlobalParam.ObjectType.Connection)
                    {
                        Connection connection2 = (Connection) this.objList[k];
                        if (connection2 == connection)
                        {
                            this.tempList.Add(connection2);
                        }
                    }
                }
            }
            for (int j = 0; j < count; j++)
            {
                o.AttachConnections.RemoveAt(0);
            }
        }

        public DrawObject GetSelectedObject(Point point)
        {
            foreach (DrawObject obj2 in this.objList)
            {
                if (obj2.PointInObject(point))
                {
                    return obj2;
                }
            }
            return null;
        }

        public DrawObject GetSelectedObject(int index)
        {
            int num = -1;
            foreach (DrawObject obj2 in this.objList)
            {
                if (obj2.Selected)
                {
                    num++;
                    if (num == index)
                    {
                        return obj2;
                    }
                }
            }
            return null;
        }

        public int GetSelectedObjectIndex()
        {
            for (int i = this.objList.Count - 1; i >= 0; i--)
            {
                DrawObject obj2 = (DrawObject) this.objList[i];
                if (obj2.Selected)
                {
                    return i;
                }
            }
            return -1;
        }

        public void MoveSelectionToFront()
        {
            if (this.GetSelectedObjectIndex() >= 0)
            {
                DrawObject obj2 = (DrawObject) this.objList[this.GetSelectedObjectIndex()];
                this.objList.RemoveAt(this.GetSelectedObjectIndex());
                this.objList.Add(obj2);
            }
        }

        public void RemoveAll()
        {
            this.objList.Clear();
        }

        public void ResetNode(Connection connect)
        {
            FBObject attachFromFB = connect.AttachFromFB;
            FBObject attachToFB = connect.AttachToFB;
            OutNodeObject obj4 = (OutNodeObject) attachFromFB.OutNodeList[connect.AttachFromNode];
            if (obj4.ConnectionCount > 0)
            {
                obj4.ConnectionCount--;
                obj4.Linked = false;
                attachFromFB.AttachConnections.Remove(connect);
            }
            InNodeObject obj5 = (InNodeObject) attachToFB.InNodeList[connect.AttachToNode];
            obj5.Linked = false;
            attachToFB.AttachConnections.Remove(connect);
        }

        public void SelectAll()
        {
            foreach (DrawObject obj2 in this.objList)
            {
                obj2.Selected = true;
            }
        }

        public void SelectInRectangle(Rectangle rectangle)
        {
            this.UnselectAll();
            foreach (DrawObject obj2 in this.objList)
            {
                if (obj2.IntersectsWith(rectangle))
                {
                    obj2.Selected = true;
                }
            }
        }

        public void UnselectAll()
        {
            foreach (DrawObject obj2 in this.objList)
            {
                obj2.Selected = false;
            }
        }

        public DrawObject this[int index]
        {
            get
            {
                if ((index < 0) || (index >= this.objList.Count))
                {
                    return null;
                }
                return (DrawObject) this.objList[index];
            }
        }

        public int SelectionCount
        {
            get
            {
                int num = 0;
                foreach (DrawObject obj2 in this.objList)
                {
                    if (obj2.Selected)
                    {
                        num++;
                    }
                }
                return num;
            }
        }
    }
}

